package com.dog.dao.impl;

import com.dog.dao.RegionDAO;
import com.dog.model.Region;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.sql.SQLException;

/**
 * User: Kuttubek Aidaraliev
 * Date: 30.04.2010
 * Time: 13:01:32
 */

@Repository
public class RegionDAOImpl extends DAOImpl<Region> implements RegionDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public RegionDAOImpl() {
        super(Region.class);
    }

    @PostConstruct
    public void initialize(){
        this.setSessionFactory(sessionFactory);
    }

    @Override
    public Region findByName(final String title) {

       return (Region)getHibernateTemplate().executeFind(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Region.class).add(Restrictions.eq("title", title));
                return criteria.uniqueResult();
            }
        });

    }
}
